%% SelectionMenu
% Selects between certains string in a GUI-oriented manner.
% Outputs a logical with the selected fields marked 1

function results = SelectionMenu(inputcell,varargin)
%% Generate stuff
if nargin < 2
    varargin{1} = 'Selection menu, make a choice';
end
    
gui.nobuttons        = [4 4];       
gui.buttondistance   = [20 10];
gui.buttonsize       = [120 22.5];
gui.figuresize       = (gui.buttonsize+gui.buttondistance).*(gui.nobuttons)+gui.buttondistance;
gui.uipositions(:,1) = sort(repmat(round(linspace(gui.buttondistance(1),gui.figuresize(1)-gui.buttondistance(1)-gui.buttonsize(1),gui.nobuttons(1))'),gui.nobuttons(2),1));
gui.uipositions(:,2) = repmat(round(linspace(gui.buttondistance(2),gui.figuresize(2)-gui.buttondistance(2)-gui.buttonsize(2),gui.nobuttons(2))'),gui.nobuttons(1),1);
gui.fid              = figure;
set(gui.fid,'name',varargin{1},'numbertitle','off')
set(gui.fid,'position',[50 50 gui.figuresize])

%% Generate boxes
for n = 1 :length(inputcell)
    gui.h.(inputcell{n}(inputcell{n}~='.'))  = uicontrol('Style','checkbox','value',0); % Plots the errors of the calibration step after calibration
end

gui.uifields          = fieldnames(gui.h);
gui.checkboxcount     = 1; n=1; m=1;
while n<=length(gui.uifields);
switch get(gui.h.(char(gui.uifields(n,:))),'Style')
    case 'checkbox'
        set(gui.h.(char(gui.uifields(n,:))),'String',char(gui.uifields(n,:)),...
            'position',[gui.uipositions(m,1) gui.uipositions(m,2) gui.buttonsize(1) gui.buttonsize(2)]);
end
    set(gui.h.(char(gui.uifields(n,:))),'Backgroundcolor',get(gui.fid,'color'));
    n=n+1; m=m+1;
end
startbutton = uicontrol('Style','togglebutton','String','Accept','position',[gui.uipositions(end,1) gui.uipositions(end,2) gui.buttonsize(1) gui.buttonsize(2)]);

while 1;
    if get(startbutton,'Value')==1
        break;
    end
    pause(0.01);
end

%% Get results
results = logical(ones(size(inputcell)));
for n = 1:length(inputcell)
    results(n) = get(gui.h.(inputcell{n}(inputcell{n}~='.')),'Value');    
end

close(gui.fid);
pause(0.05);
end